package net.easyjava.web.controller;

import net.easyjava.base.manager.ArticleManager;
import net.easyjava.tools.model.ModelBean;
import net.easyjava.tools.model.ModelConstants;
import net.easyjava.web.constants.FocusConstants;
import net.easyjava.web.constants.WebConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by wxnacy on 16/1/1.
 */
@Controller
@RequestMapping("/api")
public class ApiController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private ArticleManager articleManager;
    @Value("${article.address}")
    private String ARTICLE_ADDRESS;


    /**
     *
     * @param type  网站访问的类型:       ****：
     *                                  menu:菜单列表
     *                                  neer:最新的文章列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/{type}.json")
    @ResponseBody
    public ModelBean apiType(
            @PathVariable String type,
            HttpServletRequest request, HttpServletResponse response) {
        ModelBean modelBean = null;
        String message = ModelConstants.MESSAGE_ERROR;
        try {
            if(WebConstants.API_TYPE_MENU.equals(type)){
                /**
                 * 菜单列表
                 */
                modelBean = new ModelBean(FocusConstants.getJsonList(0));
            }else if(WebConstants.API_TYPE_NEER.equals(type)){
                /**
                 * 最新的文章列表
                 */
                modelBean = new ModelBean(articleManager.getNeerArticleList(),"id,title,createTime");
            }else{
                message = "地址有误";
                logger.error(message);
                return model(new ModelBean(message), request, response);
            }


        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            modelBean = new ModelBean(message);
        }


        return model(modelBean, request, response);
    }
}
